#!/bin/sh
#
#	$HeadURL$
#	$Revision$
#	$Date$
#

#	Basic settings, leave 'em alone in general.
CLIENT=tcp_client.pl
HOST=127.0.0.1
PASS=admin
PORTS="5000 5001 5002 5003 5004 5911"

#	If we go randomly, we're going to pick two random numbers 1 through RANDMAX.
#	If they're the same, we're going to take an action. So set RANDMAX carefully.
RANDMAX=10

#	Same goes for sleep, to make this a little more... natural, we'll sleep randomly.
SLEEPMAX=5

#	Usage
if [ -z $2 ] ; then
	echo "Usage: $0 <all|random> <once|loop>"
	echo "Which sensors to toggle, all or a random pick? and do we continue in a loop?"
	exit
fi

#	Client must be executable.
if [ ! -x $CLIENT ] ; then
	echo "Client not executable."
	exit
fi

#
#	Toggle all sensors.
#
if [ $1 = "all" ] ; then
	echo "Toggling all sensors in 2 seconds."
	sleep 2

		for PORT in $PORTS
		do
			./$CLIENT 127.0.0.1 $PORT toggle $PASS
		done

#
#	Toggle some sensors randomly.
#
elif [ $1 = "random" ] ; then

	COUNTER=0
	while [ $COUNTER -lt 1 ]; do

		for PORT in $PORTS
		do
			#	gen two random numbers 1-RANDMAX
			RAND1=$[ ($RANDOM%$RANDMAX) + 1]
			RAND2=$[ ($RANDOM%$RANDMAX) + 1]

			#	if the numbers match, toggle!
			if [ $RAND1 -eq $RAND2 ] ; then
				./$CLIENT 127.0.0.1 $PORT toggle $PASS 
			fi
		done

		#	Loop if we're told to, else get the heck out!
		if [ $2 = "loop" ]; then

			#	sleep for random interval, 1-SLEEPMAX seconds
			SLEEP=$[ ($RANDOM%$SLEEPMAX) + 1]
			echo Sleeping for $SLEEP seconds.
			sleep $SLEEP

		else 
			COUNTER=1
		fi
	done
fi

# EOF
